function async() {
    this.target = undefined
    this.status = false
    this.msg = "pending"
    this.waitTask = 0
    return this
}

async.prototype.resolve = function (value) {
    this.status = true
    this.msg = value
}

async.prototype.reject = function (value) {
    this.status = false
    this.msg = value
}

async.prototype.then = function (func) {
    if (this.status && this.waitTask == 0)
        func(this.target)
    return this
}
async.prototype.catch = function (func) {
    if (!this.status) {
        func(this.msg)
    }
    return this
}

async.prototype.asyncThen = function (func, errorFunc = undefined) {
    let self = this
    let wait = setInterval(() => {
        self.wait()
        if (self.status && self.waitTask == 0) {
            clearInterval(wait)
            self.then(func).catch((error) => {
                if (errorFunc != undefined) {
                    errorFunc(error)
                } else {
                    throw error
                }
            })
        }
    }, 0.0001);
    return this
}

async.prototype.wait = function () {
    if (this.waitTask == 0) {
        this.status = true
    }
    if (this.status) {
        this.resolve('success')
    }
    return this
}

async.prototype.next = function (func) {
    let self = this
    let waitNext = setInterval(() => {
        if (self.status) {
            clearInterval(waitNext);
            func(self.target)
        }
    }, 0.0001);
    return this
}

async.prototype.Debug = function (info = []) {
    if (!(process.env.NODE_ENV === 'production' ? true : false)) {
        info.forEach(function (o) {
            console.log("%O", o)
        });
    }
}

export default async